<?php
if (!defined('ABSPATH')) {
	define('ABSPATH', dirname(dirname(__FILE__)) . '/');
}

require_once ABSPATH . 'dr-config.php';
require_once ABSPATH . DRINC . '/dr-base.php';
require_once ABSPATH . DRINC . '/dr-functions.php';
require_once ABSPATH . DRINC . '/dr-formatting.php';

require_once ABSPATH . DRINC . '/dr-themes.php';
require_once ABSPATH . DRINC . '/mygravatar.php';

require_once ABSPATH . DRINC . '/dr-smarty.php';

require_once ABSPATH . DRINC . '/dr-page-check-login.php';

# 确认session开启
if (!isset($_SESSION)) {
	session_start();
}

# 如果用户未登陆，则跳转到登陆页面
if (! dr_is_login_on()) {
	$redirect = BASE_HOSTPATH . "dr-includes/dr-page-login.php?type=ask";
	header("Location: $redirect");
	exit;
}

# 处理用户的提问
if (dr_is_http_post() == true) {
	//获取数据
	$question['user_id'] = dr_is_login_on();

	// 获得表单数据
	$question['title']       = isset($_POST['title'])    ? $_POST['title']    : '';
	$question['content']     = isset($_POST['content'])  ? $_POST['content']  : '';
	$question['tags']        = isset($_POST['tags'])     ? $_POST['tags']     : '';
	$question['notify']     = isset($_POST['notify'])   ? 1                  : 0;

	$errors = sanitize_ask_form($question);

	if (is_null($errors)) {
		$result = save_asked_question($question);
		# 如果返回的结果不"严格"为0
		if ($result > 0) {
			# 条转到该问题的显示页面
			header("Location: " . BASE_HOSTPATH . DRINC . '/dr-page-question-answer.php?qid=' . $result);
			die;
		} else {
			echo "error_id=" . $result_array['id'];
			die;
		}
	} else {
		if (isset($errors['title'])) {
			$errors_msg = $errors['title'];
		}
		if (isset($errors['content'])) {
			if (isset($errors_msg)) {
				$errors_msg .= '<br />';
				$errors_msg .= $errors['content'];
			} else {
				$errors_msg = $errors['content'];
			}
		}
	}
}


# 页面标题
$smarty->assign('page_title', "提问 - iDaren");


if (isset($errors)) {
	$smarty->assign('errors', $errors_msg);
}
if (isset($question)) {
	$smarty->assign('question', $question);
}

$content = array();
$content['top_navs'] = get_page_top_navigation();
$content['menus'] = get_page_menus('ask');
$content['tabs'] = get_page_tabs('ask', '');
$smarty->assign('content', $content);

# 显示模板
$smarty->display('askquestion.tpl');


/* =============提问相关的函数================= */

/**
 * 过滤掉用户提交的内容中的不合法的内容
 * 
 * @param  $form
 * @return array | null
 */
function sanitize_ask_form(&$form) {
	$errors = null;
	
	$form['title'] = sanitize_plain_text($form['title']);
	if (mb_strlen($form['title'], 'UTF-8') <= 4) {
		$errors['title'] = '标题长度必须大于4。';
	} elseif (mb_strlen($form['title'], 'UTF-8') >= 100) {
		$errors['title'] = '标题长度必须小于100。';
	}

	$form['content'] = sanitize_filtered_text($form['content']);
	if (mb_strlen($form['content'], 'UTF-8') <= 15) {
		$errors['content'] = '正文长度必须大于15。';
	} elseif (mb_strlen($form['content'], 'UTF-8') >= 8000) {
		$errors['content'] = '正文长度必须小于8000。';
	}

	$form['tags'] = sanitize_plain_text($form['tags']);

	return $errors;
}

/**
 * 提交保存提问数据
 * 
 * @param  $question
 * @return bool
 */
function save_asked_question($question) {
	$posts = array(
		'action'      => 'ask_question',

		'user_id'     => $question['user_id'],
		'title'       => $question['title'],
		'content'     => $question['content'],
		'tags'        => $question['tags'],
		'is_mail'     => $question['notify'],
	);

	$result_array = json_decode(dr_server_request('dr-app-questions.php', $posts), true);

	if ($result_array['id'] > 0) {
		return $result_array['id'];
	}
	
	return false;
}

// 忽略PHP结束符
